/**
 * Created on 2007-03-06 22:20
 */
package game;

import game.media.gfx.RenderManager;

/**
 * @author David Wallin
 */
public class Matris extends BasicMatris
{
	private int					m_Xpos;
	private int					m_Ypos;
	protected RenderManager		m_RenderManager = RenderManager.INSTANCE;
	
	/**
	 * Constructor, initializes members
	 * 
	 * @param width the width of this matris
	 * @param height the height of this matris
	 * @param xPos the on screen x coordinate of this matris
	 * @param yPos the on screen y coordinate of this matris
	 */
	public Matris(int width, int height, int xPos, int yPos)
	{
		super(width, height);
		
		m_Xpos = xPos;
		m_Ypos = yPos;
		
		//register a square in the edges of the matrix, making them seem taken
		for(int x = 0; x < width; x++)
		{
			setPosition(x, 0, true);
			setPosition(x, height - 1, true);
		}
		
		for(int y = 0; y < height; y++)
		{
			setPosition(0, y, true);
			setPosition(width - 1, y, true);
		}
	}
	
	/**
	 * Checks to see if a certain shape can fit into a certain position in the matrix.
	 * Should be used to check permission to move a piece to a certain location before
	 * actually mocing it there
	 * 
	 * @param x the x coordinate in the matrix to check
	 * @param y the y coordinate in the matrix to check
	 * @param shape the shape to check
	 * 
	 * @return true if the shape can fit, otherwise false
	 */
	public boolean checkSpace(int x, int y, BasicMatris shape)
	{
		return !checkForCollision(x, y, shape);
	}
	
	/**
	 * Checks to see if a certain shape has landed in a certain position.
	 * This presumes that a shape can only move down one step at a time, or the logic
	 * of this operation will be lost.
	 * 
	 * @param x
	 * @param y
	 * @param shape
	 * @return
	 */
	public boolean checkLanding(int x, int y, BasicMatris shape)
	{
		boolean landed = false;
		
		if(y + (shape.getHeight() - 1) > (getHeight() - 1))
		{
			landed = true;
		}
		
		return landed;
	}
	
	/**
	 * Adding a shape to the matrix means to occupy certain spaces of the matrix
	 * 
	 * @param x the x coordinate to put the shape inside the matrix
	 * @param y the y coordinate to put the shape inside the matrix
	 * @param shape the shape to put at the specified coordinates
	 */
	public void addShapeToMatrix(int x, int y, BasicMatris shape)
	{
		for(int shapeY = 0; shapeY < shape.getHeight(); shapeY++)
		{
			for(int shapeX = 0; shapeX < shape.getWidth(); shapeX++)
			{
				if(shape.getPosition(shapeX, shapeY))
				{
					setPosition(x + shapeX, y + shapeY, true);
				}
				//might be the same?
				//setPosition(x + shapeX, y + shapeY, shape.getPosition(shapeX, shapeY));
			}
		}
	}
	
	private boolean checkForCollision(int x, int y, BasicMatris shape)
	{
		boolean collision = false;
		
		for(int shapeY = 0; shapeY < shape.getHeight(); shapeY++)
		{
			for(int shapeX = 0; shapeX < shape.getWidth(); shapeX++)
			{
				if((shape.getPosition(shapeX, shapeY)) && this.getPosition(x + shapeX, y + shapeY))
				{
					collision = true;
					shapeX = shape.getWidth();
					shapeY = shape.getHeight();
				}
			}
		}
		
		return collision;
	}
	
	public int getXpos()
	{
		return m_Xpos;
	}
	
	public int getYpos()
	{
		return m_Ypos;
	}
	
	public int getInnerXpos()
	{
		return getXpos() + (m_RenderManager.getSquareEntityWidth() * 1);
	}
	
	public int getInnerYpos()
	{
		return getYpos() + (m_RenderManager.getSquareEntityHeight() * 1);
	}
	
	public int getInnerX2()
	{
		return (getInnerXpos() + ((getWidth() - 2) * m_RenderManager.getSquareEntityWidth()));
	}
	
	public int getInnerY2()
	{
		return (getInnerYpos() + ((getHeight() - 2) * m_RenderManager.getSquareEntityHeight()));
	}
	
	public int getInnerPixelWidth()
	{
		return getInnerX2() - getInnerXpos();
	}
	
	public int getInnerPixelHeight()
	{
		return getInnerY2() - getInnerYpos();
	}
}
